﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class EditPassword : Form {

        public EditPassword( ) {
            InitializeComponent( );
        }

        private void button1_Click(object sender, EventArgs e) {
            if (GlobalSetting.CurrentSystemUser != null) {
                var p0 = textBox3.Text.Trim();
                if (GlobalSetting.CurrentSystemUser.Password != SystemUser.MD5Pwd(p0)) {
                    FormHelper.ShowErrorMsg("旧密码不正确");
                    return;
                }
                var p1 = textBox1.Text.Trim();
                var p2 = textBox1.Text.Trim();
                if (!string.IsNullOrWhiteSpace(p1) && p1 == p2) {
                    GlobalSetting.CurrentSystemUser.Password = SystemUser.MD5Pwd(p1);
                    (new SystemUserManage()).Update(GlobalSetting.CurrentSystemUser);
                    FormHelper.ShowInfoMsg("修改密码成功，用你的新密码登录吧");
                    Close();
                }
                else {
                    FormHelper.ShowErrorMsg("两次输入的密码不一致");
                }

                return;
            }
            else {
                FormHelper.ShowErrorMsg("请先登录！");
                this.Close( );
            }
           
           
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void textBox1_Leave(object sender, EventArgs e) {
            var npwd = textBox1.Text.Trim();
            if (npwd.Length > 8) {
                label4.Text = "密码长度不能超过8";
                button1.Enabled = false;
            }
            else if (npwd.Length == 0) {
                label4.Text = "新密码不能为空";
                button1.Enabled = false;
            }
            else {
                button1.Enabled = true;
            }
        }

        private void textBox2_Leave(object sender, EventArgs e) {
            var cnpwd = textBox2.Text.Trim();
            var npwd = textBox1.Text.Trim( );
            if (cnpwd != npwd) {
                label5.Text = "两次输入密码不一致";
                button1.Enabled = false;
            }
            else {
                button1.Enabled = true;
            }
        }

    
    }
}
